package com.szj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szj.entity.Clazz;
import com.szj.entity.Teacher;
import com.szj.service.StudentService;
import com.szj.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @Autowired
    private StudentService studentService;

    private Map<String,Object> result=new HashMap<String,Object>();

    /**
     * 跳转到老师页面
     * @return
     */
    @GetMapping("/goTeacherListView")
    public ModelAndView goTeacherListView(ModelAndView modelAndView){
        //向页面发送一个存储Grade的List对象
        modelAndView.addObject("clazzList", studentService.ClazzNameAll());
        //设置返回的页面
        modelAndView.setViewName("teacher/teacherList");
        //返回modelAndView
        return modelAndView;
    }

    /**
     * 添加老师信息
     *
     * @param teacher
     * @return
     */
    @PostMapping("/addTeacher")
    @ResponseBody
    public Map<String, Object> addTeacher(Teacher teacher) {
        System.out.println(teacher);
        //判断老师称是否存在
        Teacher name = teacherService.findByName(teacher.getName());
        if (name == null) {
            //添加操作
            if (teacherService.teacherAdd(teacher) > 0) {
                result.put("success", true);
            } else {
                //失败
                result.put("success", false);
                result.put("msg", "添加失败,服务器异常!");
            }
        } else {
            //添加失败
            result.put("success", false);
            result.put("msg", "添加失败,请修改老师名再重试!");
        }
        return result;

    }

    /**
     * 删除老师信息
     * @return
     */
    @PostMapping("deleteTeacher")
    @ResponseBody
    public Map<String,Object> deleteTeacher(@RequestParam("ids[]") Integer[] ids) {
        if (teacherService.teacherDel(ids)>0){
            result.put("success",true);
        }
        else{
            result.put("success",false);
        }
        return result;
    }

    /**
     * 修改老师信息
     *
     * @param teacher
     * @return
     */
    @PostMapping("/editTeacher")
    @ResponseBody
    public Map<String, Object> editTeacher(Teacher teacher) {
        //1.排除用户名只修改班级名以外信息
        Teacher t = teacherService.findByName(teacher.getName());
        if (t != null) {
            if (!(teacher.getId().equals(t.getId()))) {
                result.put("success", false);
                result.put("msg", "该老师名已存在,请修老师名再重试!");
                return result;
            }
        }
        try {
            //修改操作
            if (teacherService.teacherUp(teacher) > 0) {
                result.put("success", true);
            } else {
                result.put("success", false);
                result.put("msg", "修改失败,服务器异常!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 分页查询,根据老师名称和班级名称查询指定或者所有年级列表信息
     * @param page
     * @param rows
     * @param clazzname
     * @param teachername
     * @return
     */
    @PostMapping("/getTeacherList")
    @ResponseBody
    public Map<String, Object> getTeacherList(Integer page, Integer rows, String clazzname,String teachername) {
        Teacher teacher = new Teacher();
        teacher.setName(teachername);
        teacher.setClazz_name(clazzname);
        System.out.println(clazzname);
        System.out.println(teachername);
        //设置每页记录数
        PageHelper.startPage(page, rows);
        //根据年级名称查询指定或者所有年级列表信息
        //调用gradeService完成
        List<Teacher> list = teacherService.selectList(teacher);
        //封装信息
        PageInfo<Teacher> pageInfo = new PageInfo<>(list);
        //获取总记录数
        long total = pageInfo.getTotal();
        //获取当前数据列表
        List<Teacher> gradeList = pageInfo.getList();
        //存储对象信息
        result.put("total", total);
        result.put("rows", gradeList);
        return result;
    }

}
